package de.unikoblenz.west.csxpoi;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.Calendar;
import java.util.TimeZone;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.impl.client.DefaultHttpClient;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

import de.unikoblenz.west.csxpoi.R;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;

/**
 * The activity for editing POIs.
 */
public class PoiEditorActivity extends Activity {

	/**
	 * the constant representing the saving dialog
	 */
	private static final int SAVING_DIALOG = 1;

	/**
	 * the constant representing the request for the ontology editor activity
	 */
	private static final int ONTOLOGY_EDITOR_REQUEST = 1;

	/**
	 * the constant representing the request for the date time picker activity
	 */
	private static final int DATE_TIME_PICKER_REQUEST = 2;

	/**
	 * the POI before editing
	 */
	private PoiWrapper mOldPoi = null;

	/**
	 * the POI after editing
	 */
	private PoiWrapper mNewPoi = null;

	/**
	 * the ID of the editing user
	 */
	private String mUserId = null;

	/**
	 * a flag whether to save the POI after adding the last category
	 */
	private boolean mSaveFlag = false;

	/**
	 * {@link TextView}s showing the text and date
	 */
	private TextView mFromTextView, mUntilTextView, mFromTextNameView,
			mUntilTextNameView;

	/**
	 * edit date {@link ImageButton}
	 */
	private ImageButton mEditFromButton, mEditUntilButton;

	/**
	 * preset {@link Button}s
	 */
	private Button mTodayButton, mTomorrowButton, mWeekButton;

	/**
	 * Called when the activity is started.
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_NO_TITLE);

		setContentView(R.layout.poi_editor);

		Bundle extras = getIntent().getExtras();

		mUserId = extras.getString("user");

		mOldPoi = extras.getParcelable("poi");

		if (mOldPoi == null) {
			mNewPoi = new PoiWrapper();
			mNewPoi.setName("");
			mNewPoi.setLatitude(extras.getDouble("latitude"));
			mNewPoi.setLongitude(extras.getDouble("longitude"));
		} else {
			mNewPoi = new PoiWrapper(mOldPoi);
		}

		// Categories:
		ArrayAdapter<CategoryWrapper> categoriesAdapter = new ArrayAdapter<CategoryWrapper>(
				this, android.R.layout.simple_dropdown_item_1line,
				OntologyBuffer.getCategories());
		final AutoCompleteTextView newCategoryView = ((AutoCompleteTextView) findViewById(R.id.new_category));
		final EditText nameView = (EditText) findViewById(R.id.name);
		nameView.setText(mNewPoi.getName());
		newCategoryView.setAdapter(categoriesAdapter);

		// Plus button:
		final ImageButton plusButton = (ImageButton) findViewById(R.id.plus_button);
		plusButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				String categoryName = newCategoryView.getText().toString();
				if (!categoryName.equals("")) {
					newCategoryView.setText("");
					addCategory(CategoryWrapper.createFromName(categoryName));
					updateView();
				}
			}
		});

		// Cancel button:
		final Button cancelButton = (Button) findViewById(R.id.cancel_button);
		cancelButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				finish();
			}
		});

		// Save button:
		final Button saveButton = (Button) findViewById(R.id.save_button);
		saveButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				String name = nameView.getText().toString();
				if (!name.equals("")) {
					mNewPoi.setName(name);

					String categoryName = newCategoryView.getText().toString();
					if (!categoryName.equals("")) {
						mSaveFlag = true;
						addCategory(CategoryWrapper
								.createFromName(categoryName));
					} else {
						new AsyncSave().execute();
					}
				}
			}
		});

		// Delete button:
		final Button deleteButton = (Button) findViewById(R.id.delete_button);
		if (mOldPoi == null) {
			deleteButton.setVisibility(View.GONE);
		} else {
			deleteButton.setOnClickListener(new View.OnClickListener() {
				public void onClick(View v) {
					new AsyncDelete().execute();
				}
			});
		}

		mFromTextView = (TextView) findViewById(R.id.from_text);
		mUntilTextView = (TextView) findViewById(R.id.until_text);
		mFromTextNameView = (TextView) findViewById(R.id.from_text_name);
		mUntilTextNameView = (TextView) findViewById(R.id.until_text_name);
		mEditFromButton = (ImageButton) findViewById(R.id.edit_from_button);
		mEditUntilButton = (ImageButton) findViewById(R.id.edit_until_button);

		mTodayButton = (Button) findViewById(R.id.today_button);
		mTomorrowButton = (Button) findViewById(R.id.tomorrow_button);
		mWeekButton = (Button) findViewById(R.id.week_button);

		mTodayButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// starting now
				String start = Helpers.getTodayStart();
				mFromTextView.setText(Helpers
						.formatIsoStringToNiceFormat(start));
				mNewPoi.setStartDate(start);
				// until the end of today
				String end = Helpers.getTodayEnd();
				mUntilTextView
						.setText(Helpers.formatIsoStringToNiceFormat(end));
				mNewPoi.setEndDate(end);
			}
		});

		mTomorrowButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// starting tomorrow 00:00
				String start = Helpers.getTomorrowStart();
				mFromTextView.setText(Helpers
						.formatIsoStringToNiceFormat(start));
				mNewPoi.setStartDate(start);
				// until tomorrow 23:59
				String end = Helpers.getTomorrowEnd();
				mUntilTextView
						.setText(Helpers.formatIsoStringToNiceFormat(end));
				mNewPoi.setEndDate(end);
			}
		});

		mWeekButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// starting now
				String start = Helpers.getCurrentDateTimeXsdConform();
				mFromTextView.setText(Helpers
						.formatIsoStringToNiceFormat(start));
				mNewPoi.setStartDate(start);
				// until today + one week
				String end = Helpers.getOneWeekEnd();
				mUntilTextView
						.setText(Helpers.formatIsoStringToNiceFormat(end));
				mNewPoi.setEndDate(end);
			}
		});

		final CheckBox withTimeCheckBox = (CheckBox) findViewById(R.id.with_date_time_checkbox);

		if (mNewPoi.isEvent())
			withTimeCheckBox.setChecked(true);
		withTimeCheckBox
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {
					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						// show date / time
						if (isChecked) {
							setEventVisibility(View.VISIBLE);

							if (mNewPoi.getStartDate().equals("")) {
								String currentDate = Helpers
										.getCurrentDateTimeXsdConform();
								mFromTextView
										.setText(Helpers
												.formatIsoStringToNiceFormat(currentDate));
								mNewPoi.setStartDate(currentDate);
							} else {
								mFromTextView.setText(Helpers
										.formatIsoStringToNiceFormat(mNewPoi
												.getStartDate()));
							}
							if (mNewPoi.getEndDate().equals("")) {
								String tomorrowDate = Helpers.getTodayEnd();
								mUntilTextView
										.setText(Helpers
												.formatIsoStringToNiceFormat(tomorrowDate));
								mNewPoi.setEndDate(tomorrowDate);
							} else {
								mUntilTextView.setText(Helpers
										.formatIsoStringToNiceFormat(mNewPoi
												.getEndDate()));
							}
						}
						// remove date / time
						else {
							setEventVisibility(View.GONE);
							mNewPoi.setStartDate("");
							mNewPoi.setEndDate("");
						}
					}
				});

		if (mOldPoi == null) { // new poi
			setEventVisibility(View.GONE);
			mEditFromButton.setOnClickListener(new View.OnClickListener() {
				public void onClick(View v) {
					showDateTimePickerDialog("Set start date time", true);
				}
			});
			mEditUntilButton.setOnClickListener(new View.OnClickListener() {
				public void onClick(View v) {
					showDateTimePickerDialog("Set end date time", false);
				}
			});
		} else { // existing poi
			setEventVisibility(View.GONE);
			mEditFromButton.setOnClickListener(new View.OnClickListener() {
				public void onClick(View v) {
					showDateTimePickerDialog("Set start date time", true);
				}
			});
			mEditUntilButton.setOnClickListener(new View.OnClickListener() {
				public void onClick(View v) {
					showDateTimePickerDialog("Set end date time", false);
				}
			});

			if (mOldPoi.isEvent()) { // editing an event
				setEventVisibility(View.VISIBLE);
				mFromTextView.setText(Helpers
						.formatIsoStringToNiceFormat(mOldPoi.getStartDate()));
				mUntilTextView.setText(Helpers
						.formatIsoStringToNiceFormat(mOldPoi.getEndDate()));
			} else { // editing a poi
				setEventVisibility(View.GONE);
			}
		}
	}

	/**
	 * setEventVisibility sets the visibility of all event-corresponding widgets
	 * to the given status.
	 * 
	 * @param visibility
	 *            either {@link View}.VISIBLE, {@link View}.INVISIBLE or
	 *            {@link View}.GONE
	 */
	private void setEventVisibility(int visibility) {
		mFromTextView.setVisibility(visibility);
		mUntilTextView.setVisibility(visibility);
		mFromTextNameView.setVisibility(visibility);
		mUntilTextNameView.setVisibility(visibility);
		mEditFromButton.setVisibility(visibility);
		mEditUntilButton.setVisibility(visibility);
		mTodayButton.setVisibility(visibility);
		mTomorrowButton.setVisibility(visibility);
		mWeekButton.setVisibility(visibility);
	}

	/**
	 * Called when the activity is brought back to the front.
	 */
	@Override
	protected void onResume() {
		super.onResume();
		updateView();
	}

	/**
	 * showDateTimePickerDialog shows a date time picker dialog with the given
	 * caption. The date and time, shown in this dialog is the start date and
	 * time of the poi, if "from" is true, or else the end date and time of the
	 * poi.
	 * 
	 * @param caption
	 *            String
	 * @param from
	 *            boolean true if start date, false if end date
	 */
	private void showDateTimePickerDialog(String caption, boolean from) {
		Intent dateTimePickerIntent = new Intent(this,
				DateTimePickerActivity.class);
		dateTimePickerIntent.putExtra("caption", caption);
		dateTimePickerIntent.putExtra("from", from);

		Calendar cal;
		if (from) { // it is the start date
			cal = Helpers.getCalendarOfIsoString(mNewPoi.getStartDate());
		} else { // it is the end date
			cal = Helpers.getCalendarOfIsoString(mNewPoi.getEndDate());
		}
		dateTimePickerIntent.putExtra("year", cal.get(Calendar.YEAR));
		dateTimePickerIntent.putExtra("month", cal.get(Calendar.MONTH));
		dateTimePickerIntent.putExtra("day", cal.get(Calendar.DAY_OF_MONTH));
		dateTimePickerIntent.putExtra("hour", cal.get(Calendar.HOUR_OF_DAY));
		dateTimePickerIntent.putExtra("minutes", cal.get(Calendar.MINUTE));

		startActivityForResult(dateTimePickerIntent, DATE_TIME_PICKER_REQUEST);
	}

	/**
	 * Updates the variable parts of the view.
	 */
	private void updateView() {
		final LinearLayout categoryItemsContainer = (LinearLayout) findViewById(R.id.removable_category_items_container);
		categoryItemsContainer.removeAllViews();
		for (final CategoryWrapper category : mNewPoi.getCategories()) {
			final LinearLayout categoryItemView = (LinearLayout) View.inflate(
					PoiEditorActivity.this, R.layout.removable_category_item,
					null);
			final TextView categoryView = (TextView) categoryItemView
					.findViewById(R.id.tag);
			categoryView.setText(category.getName());
			final ImageButton minusButton = (ImageButton) categoryItemView
					.findViewById(R.id.minus_button);
			minusButton.setOnClickListener(new View.OnClickListener() {
				public void onClick(View v) {
					mNewPoi.removeCategory(category);
					updateView();
				}
			});
			categoryItemsContainer.addView(categoryItemView);
		}
	}

	/**
	 * Adds a category to the POI. If it has no URI, the ontology editor is
	 * started to create a new instance.
	 * 
	 * @param category
	 *            the category to add
	 */
	private void addCategory(CategoryWrapper category) {
		if (category.getUri() == null) {
			Intent ontologyEditorIntent = new Intent(this,
					OntologyEditorActivity.class);
			ontologyEditorIntent.putExtra("user", mUserId);
			ontologyEditorIntent.putExtra("name", category.getName());
			startActivityForResult(ontologyEditorIntent,
					ONTOLOGY_EDITOR_REQUEST);
		} else {
			mNewPoi.addCategory(category);
			if (mSaveFlag)
				new AsyncSave().execute();
		}
	}

	/**
	 * Called when an activity returns a result.
	 */
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == RESULT_OK) {
			Bundle extras = data.getExtras();
			switch (requestCode) {
			case ONTOLOGY_EDITOR_REQUEST:
				if (extras != null) {
					CategoryWrapper category = extras.getParcelable("category");
					if (category != null) {
						mNewPoi.addCategory(category);
						if (mSaveFlag)
							new AsyncSave().execute();
					}
				}
				break;
			case DATE_TIME_PICKER_REQUEST:
				if (extras != null) {
					int year = extras.getInt("year");
					int month = extras.getInt("month") + 1;
					int day = extras.getInt("day");
					int hour = extras.getInt("hour");
					int minutes = extras.getInt("minutes");
					boolean from = extras.getBoolean("from");
					if (from) { // start date time
						final TextView fromTextView = (TextView) findViewById(R.id.from_text);
						String startDate = Helpers.formatToIsoDateTimeString(
								year, month, day, hour, minutes);
						fromTextView.setText(Helpers
								.formatIsoStringToNiceFormat(startDate));
						mNewPoi.setStartDate(startDate);
					} else { // end date time
						final TextView untilTextView = (TextView) findViewById(R.id.until_text);
						String endDate = Helpers.formatToIsoDateTimeString(
								year, month, day, hour, minutes);
						untilTextView.setText(Helpers
								.formatIsoStringToNiceFormat(endDate));
						mNewPoi.setEndDate(endDate);
					}
				}
				break;
			}
		}
	}

	/**
	 * Called when a dialog is shown.
	 */
	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case SAVING_DIALOG:
			ProgressDialog savingDialog = new ProgressDialog(this);
			savingDialog.setTitle(R.string.saving_dialog_title);
			savingDialog.setMessage(getResources().getString(
					R.string.please_wait_text));
			savingDialog.setIndeterminate(true);
			savingDialog.setCancelable(true);
			return savingDialog;
		}
		return super.onCreateDialog(id);
	}

	/**
	 * Asynchronously saves the POI.
	 */
	private class AsyncSave extends AsyncTask<Void, Void, Boolean> {

		/**
		 * Called before the asynchronous saving of the POI.
		 */
		@Override
		protected void onPreExecute() {
			showDialog(SAVING_DIALOG);
		}

		/**
		 * Called when starting the asynchronous saving of the category.
		 */
		@Override
		protected Boolean doInBackground(Void... args) {

			boolean success = true;

			
			try {
				HttpClient client = new DefaultHttpClient();

				if (mOldPoi == null) { // new POI
					double altitude = 0;
					try {
						URL url = new URL("http://ws.geonames.org/srtm3?lat="
								+ mNewPoi.getLatitude() + "&lng="
								+ mNewPoi.getLongitude());
						BufferedReader in = new BufferedReader(
								new InputStreamReader(url.openStream()));
						String line;
						if ((line = in.readLine()) != null) {
							altitude = Double.valueOf(line);
						}
					} catch (MalformedURLException e) {
						altitude = 0;
					} catch (UnknownHostException e) {
						altitude = 0;
					} catch (NumberFormatException e) {
						altitude = 0;
					} catch (IOException e) {
						altitude = 0;
					}
					String urlString = Constants.REST_ADDRESS + "pois"
							+ "?name=" + Encoder.urlEncode(mNewPoi.getName())
							+ "&user=" + Encoder.urlEncode(mUserId) + "&lat="
							+ mNewPoi.getLatitude() + "&long="
							+ mNewPoi.getLongitude() + "&alt=" + altitude
							+ "&start=" + mNewPoi.getStartDate() + "&end="
							+ mNewPoi.getEndDate();
					for (int i = 0; i < mNewPoi.getCategories().size(); i++) {
						CategoryWrapper category = mNewPoi.getCategories().get(
								i);
						urlString += "&category" + (i + 1) + "="
								+ Encoder.urlEncode(category.getUri());
					}
					
					System.out.println(urlString);
					
					HttpPost request = new HttpPost(urlString);
					HttpResponse response = client.execute(request);
					if (response.getStatusLine().getStatusCode() != 404) {
						SAXParserFactory spf = SAXParserFactory.newInstance();
						SAXParser sp = spf.newSAXParser();
						XMLReader xr = sp.getXMLReader();
						PoiResponseHandler poiResponseHandler = new PoiResponseHandler();
						xr.setContentHandler(poiResponseHandler);
						xr.parse(new InputSource(response.getEntity()
								.getContent()));
						PoiWrapper poi = poiResponseHandler.getPoi();
						if (poi != null) {
							mNewPoi = poi;
							success = true;
						} else {
							success = false;
						}
					} else {
						success = false;
					}

				} else { // existing POI
					String urlBase = Constants.REST_ADDRESS + "pois/"
							+ Encoder.urlEncode(mNewPoi.getUri()) + "?user="
							+ Encoder.urlEncode(mUserId);

					// changed name
					if (!mNewPoi.getName().equals(mOldPoi.getName())) {
						String urlString = urlBase + "&changes="
								+ Encoder.urlEncode(Constants.URI_RDFS_LABEL)
								+ "&value="
								+ Encoder.urlEncode(mNewPoi.getName());
						
						System.out.println(urlString);
						
						HttpPut request = new HttpPut(urlString);
						HttpResponse response = client.execute(request);
						if (response.getStatusLine().getStatusCode() != 404) {
							// TODO: parse response (success/failure)
							mOldPoi.setName(mNewPoi.getName());
							success &= true;
						} else {
							success = false;
						}
					}

					// changed / added start date
					if (!mOldPoi.getStartDate().equals(mNewPoi.getStartDate())) {
						String urlString = urlBase
								+ "&changes="
								+ Encoder
										.urlEncode(Constants.URI_HAS_START_DATETIME)
								+ "&value="
								+ Encoder.urlEncode(mNewPoi.getStartDate());
						
						System.out.println(urlString);
						
						HttpPut request = new HttpPut(urlString);
						HttpResponse response = client.execute(request);
						if (response.getStatusLine().getStatusCode() != 404) {
							mOldPoi.setStartDate(mNewPoi.getStartDate());
							success &= true;
						} else {
							success = false;
						}
					}

					// changed / added end date
					if (!mOldPoi.getEndDate().equals(mNewPoi.getEndDate())) {
						String urlString = urlBase
								+ "&changes="
								+ Encoder
										.urlEncode(Constants.URI_HAS_END_DATETIME)
								+ "&value="
								+ Encoder.urlEncode(mNewPoi.getEndDate());
						
						System.out.println(urlString);
						
						HttpPut request = new HttpPut(urlString);
						HttpResponse response = client.execute(request);
						if (response.getStatusLine().getStatusCode() != 404) {
							mOldPoi.setEndDate(mNewPoi.getEndDate());
							success &= true;
						} else {
							success = false;
						}
					}

					// check categories
					for (CategoryWrapper category : mNewPoi.getCategories()) {
						// added category
						if (!mOldPoi.getCategories().contains(category)) {
							String urlString = urlBase + "&adds="
									+ Encoder.urlEncode(Constants.URI_RDF_TYPE)
									+ "&value="
									+ Encoder.urlEncode(category.getUri());
							
							System.out.println("Adding Categories: "+urlString);
							
							HttpPut request = new HttpPut(urlString);
							HttpResponse response = client.execute(request);
							if (response.getStatusLine().getStatusCode() != 404) {
								success &= true;
							} else {
								success = false;
							}
						}
					}
					for (CategoryWrapper category : mOldPoi.getCategories()) {
						// deleted category
						if (!mNewPoi.getCategories().contains(category)) {
							String urlString = urlBase + "&deletes="
									+ Encoder.urlEncode(Constants.URI_RDF_TYPE)
									+ "&value="
									+ Encoder.urlEncode(category.getUri());
							
							System.out.println("Remove Categories: "+urlString);
							
							HttpPut request = new HttpPut(urlString);
							HttpResponse response = client.execute(request);
							if (response.getStatusLine().getStatusCode() != 404) {
								success &= true;
							} else {
								success = false;
							}
						}
					}
					mOldPoi.setCategories(mNewPoi.getCategories());
				}
			} catch (UnsupportedEncodingException e) {
				success = false;
			} catch (ClientProtocolException e) {
				success = false;
			} catch (IOException e) {
				success = false;
			} catch (ParserConfigurationException e) {
				success = false;
			} catch (SAXException e) {
				success = false;
			}
			return success;
		}

		/**
		 * Called after asynchronously saving the POI.
		 */
		@Override
		protected void onPostExecute(Boolean success) {
			dismissDialog(SAVING_DIALOG);
			Toast.makeText(
					PoiEditorActivity.this,
					success ? R.string.edit_poi_success_message
							: R.string.edit_poi_error_message,
					Toast.LENGTH_LONG).show();
			if (success) {
				Intent resultIntent = new Intent(PoiEditorActivity.this,
						mOldPoi == null ? CsxPoiActivity.class
								: PoiInspectorActivity.class);
				resultIntent.putExtra("poi", mNewPoi);
				setResult(RESULT_OK, resultIntent);
			}
			finish();
		}
	}

	/**
	 * Asynchronously deletes the POI.
	 */
	private class AsyncDelete extends AsyncTask<Void, Void, Boolean> {

		/**
		 * Called before the asynchronous deleting the POI.
		 */
		@Override
		protected void onPreExecute() {
			showDialog(SAVING_DIALOG);
		}

		/**
		 * Called when starting the asynchronous deleting of the POI.
		 */
		@Override
		protected Boolean doInBackground(Void... args) {

			try {
				String urlString = Constants.REST_ADDRESS + "pois/"
						+ Encoder.urlEncode(mNewPoi.getUri()) + "?user="
						+ Encoder.urlEncode(mUserId);
				HttpClient client = new DefaultHttpClient();
				HttpDelete request = new HttpDelete(urlString);
				HttpResponse response = client.execute(request);
				if (response.getStatusLine().getStatusCode() != 404) {
					return true;
				} else {
					return false;
				}
			} catch (UnsupportedEncodingException e) {
				return false;
			} catch (ClientProtocolException e) {
				return false;
			} catch (IOException e) {
				return false;
			}
		}

		/**
		 * Called after the asynchronous deleting of the POI.
		 */
		@Override
		protected void onPostExecute(Boolean success) {
			dismissDialog(SAVING_DIALOG);
			Toast.makeText(
					PoiEditorActivity.this,
					success ? R.string.delete_poi_success_message
							: R.string.delete_poi_error_message,
					Toast.LENGTH_LONG).show();
			if (success) {
				Intent resultIntent = new Intent(PoiEditorActivity.this,
						PoiInspectorActivity.class);
				resultIntent.putExtra("poi", mNewPoi);
				setResult(RESULT_OK, resultIntent);
			}
			finish();
		}
	}
}
